package org.hako.recite.word.tool.writer;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;

import org.hako.recite.word.parser.WordList;
import org.hako.recite.word.util.IOUtils;

/**
 * Abstract implement of {@link WordListFileWriter}.
 * 
 * @since 0.4.9
 * @author <a href="mailto:xnnyygn@gmail.com">xnnyygn</a>
 */
public abstract class AbstractWordListFileWriter implements WordListFileWriter {

	@Override
	public void write(WordList wordList, String filePath)
			throws WordListWriteException {
		write(wordList, new File(filePath));
	}

	@Override
	public void write(WordList wordList, File file)
			throws WordListWriteException {
		FileOutputStream outputStream = null;
		try {
			outputStream = new FileOutputStream(file);
			write(wordList, outputStream);
		} catch (FileNotFoundException e) {
			throw new WordListWriteException(e);
		} catch (WordListWriteException e) {
			throw e;
		} finally {
			IOUtils.closeQuietly(outputStream);
		}
	}

}
